package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XuChengException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.mapper.CourseTeacherMapperStruct;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author luhao
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Autowired
    private CourseTeacherMapperStruct courseTeacherMapperStruct;

    @Override
    public List<CourseTeacher> findCourseTeacherByCourseId(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeachers = baseMapper.selectList(wrapper);
        return courseTeachers;
    }

    @Override
    @Transactional
    public CourseTeacher addOrAmendCourseTeacher(Long companyId, AddCourseTeacherDto dto) {
        // 根据课程id查询课程信息
        CourseBase courseBase = courseBaseMapper.selectById(dto.getCourseId());
        // 获取机构信息
        Long queryCompanyId = courseBase.getCompanyId();
        if (!queryCompanyId.equals(companyId)) {
            XuChengException.cast("其他机构无法添加课程");
        }
        CourseTeacher courseTeacher = courseTeacherMapperStruct.toCourseTeacher(dto);
        // 判断是添加还是修改
        if (dto.getId() != null) {
            int updateById = baseMapper.updateById(courseTeacher);
            if (updateById <= 0) {
                XuChengException.cast("修改课程失败");
            }
        } else {
            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = baseMapper.insert(courseTeacher);
            if (insert <= 0) {
                XuChengException.cast("添加课程失败");
            }
        }
        CourseTeacher queryCourseTeacher = baseMapper.selectById(courseTeacher.getId());
        return queryCourseTeacher;
    }

    @Override
    public boolean removeCourseTeacher(Long courseId, Long courseTeacherId, Long companyId) {
        // 根据课程id查询出课程基本信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        // 获取机构id
        Long queryCompanyId = courseBase.getCompanyId();
        if (!companyId.equals(queryCompanyId)) {
            XuChengException.cast("其他机构无法删除本机构老师");
        }
        // 删除本机构老师
        int deleteById = baseMapper.deleteById(courseTeacherId);
        return deleteById > 0;
    }
}
